跳到主要内容

Unity 中使用 “注解” - Attribute

Unity 的 Attribute

C# 也有类似 Java注解的东西 Attribute,使用方法也和 Java很相似

[Header("移动参数")]
public float speed = 8f;

变量相关

// SerializeField:在变量前面使用这个属性,可以强制改变量进行序列化。
// 即可以在 Inspector 上对变量进行编辑,即使变量的类型是 Private 的也可以
// 注意,这里只是说可以在 Inspector 面板上对变量进行编辑,但是变量的类型还是 Private 的。
[SerializeField]
private string str;

// HideInInspector:在变量上使用这个属性,可以让public的变量在Inspector上隐藏,
// 也就是无法在Editor中进行编辑,同样,这里只是不能在Inspector面板上对变量进行编辑,但是变量的类型还是Public的。
[HideInInspector]
public string str;

// Header 就是在属性前面加个 Header
[Header("移动参数")]
public float speed = 8f;

// Tooltip 可以为属性添加一个提示(鼠标移上去显示)
[Tooltip("移动参数")]
public float speed = 8f;

这个 Header 的效果 image.png

Component 相关

// 它的作用就是添加该脚本时,会自动将所依赖的各个组件添加至 GamObject 上,避免人为操作的失误
[RequireComponent(typeof(Rigidbody),typeof(test2))]
public class test1 : MonoBehaviour {
}

// 上面那个最大放三个,可以使用多个 RequireComponent 突破限制
[RequireComponent(typeof(PlayerBasicModel))]
[RequireComponent(typeof(PlayerViewModel))]
[RequireComponent(typeof(PlayerStateModel))]
[RequireComponent(typeof(PlayerInputModel))]
public class test1 : MonoBehaviour {
}


// 限制一个对象只能拥有一个此 Component
[DisallowMultipleComponent]
public class test1 : MonoBehaviour {
}

// 让自己撰写的 Component 脚本,出现在 "Add Component" 菜单中。
[AddComponentMenu(string)]
public class test1 : MonoBehaviour {
}

// 本来 Update() 等 message 接收函式只会在 Play Mode(游戏执行中)才能执行,
// 加上这个 Attribute 则可以在 Editor Mode (编辑器中) 运用 Update() 等部分函式。
// 细节参考文档: https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
[ExecuteInEditMode]
public class test1 : MonoBehaviour {
}

Class / Struct 修饰

// 将类别 (class) 或结构 (struct) 设置成 Serializable 的类型,
// 则可以被 Unity 自动序列化显示于 Inspector 上,或者使用 [SerializeField] 强制序列化显示。
[System.Serializable]
public class SerializableClass {
public int NumberInClass;
public string StringInClass;
}

public SerializableClass m_SerializableClass;

静态加载

InitializeOnLoad 在 Class 上使用,可以在 Unity启动的时候,运行 Editor脚本。需要该 Class拥有静态的构造函数。

[InitializeOnLoad]
public class ScriptExecuteOrderSetting
{
// 在 Method 上使用,是 InitializeOnLoad 的 Method 版本。Method 必须是 static 的。
static ScriptExecuteOrderSetting()
{
UnityEngine.Debug.Log("ScriptExecuteOrderSetting");
}
}

普通菜单相关

参考资料 Unity中常用的Attribute及其使用

Project 面板右键菜单

#region 右键菜单 导出资源包(选中资源时有效)

[MenuItem("Assets/导出Unity资源包", true)]
static bool ExportPackageValidation()
{
for (var i = 0; i < Selection.objects.Length; i++)
{
if (AssetDatabase.GetAssetPath(Selection.objects[i]) != "")
return true;
}

return false;
}

[MenuItem("Assets/导出Unity资源包")]
static void ExportPackage()
{
var path = EditorUtility.SaveFilePanel("Save unitypackage", "", "", "unitypackage");
if (path == "")
return;
UnityEngine.Debug.Log("路径:" + path);
var assetPathNames = new string[Selection.objects.Length];
for (var i = 0; i < assetPathNames.Length; i++)
{
assetPathNames[i] = AssetDatabase.GetAssetPath(Selection.objects[i]);
}

assetPathNames = AssetDatabase.GetDependencies(assetPathNames);

AssetDatabase.ExportPackage(assetPathNames, path,
ExportPackageOptions.Interactive | ExportPackageOptions.Recurse | ExportPackageOptions.IncludeDependencies);
}

#endregion

Hierarchy 右键菜单

[MenuItem("GameObject/MenuTest/MenuTest1", false, -2)]
static void RightHierarchy()
{
Debug.Log("Hierarchy右键菜单");
}

Inspector 面板

ContextMenu 可以在对应脚本的 Inspector 的中增加右键菜单选项。

[ContextMenu("菜单项")]
void DoSomething()
{
Debug.Log("脚本上的菜单项");
}

ContextMenuItem 在 Inspector 上面对变量追加一个右键菜单。

[ContextMenuItem("Reset", "ResetValue")]
public string value = "Default";
void ResetValue()
{
value = "Default";
}